// MatlabEngine.cpp: implementation of the CMatlabEng class.
//
//////////////////////////////////////////////////////////////////////


//#include "stdafx.h"
#include <wx/wx.h>
#include "MatlabEng.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMatlabEng::CMatlabEng()
{
	pEng=NULL;
	hinstLib = LoadLibrary(_("libeng.dll"));
	if (hinstLib != NULL)
	{
		pengOpen			= (hengOpen)			GetProcAddress(hinstLib, "engOpen");
		pengClose			= (hengClose)			GetProcAddress(hinstLib, "engClose");
		pengGetVariable		= (hengGetVariable)		GetProcAddress(hinstLib, "engGetVariable");
		pengGetVisible		= (hengGetVisible)		GetProcAddress(hinstLib, "engGetVisible");
		pengOpenSingleUse	= (hengOpenSingleUse)	GetProcAddress(hinstLib, "engOpenSingleUse");
		pengPutVariable		= (hengPutVariable)		GetProcAddress(hinstLib, "engPutVariable");
		pengSetVisible		= (hengSetVisible)		GetProcAddress(hinstLib, "engSetVisible");
		pengEvalString		= (hengEvalString)		GetProcAddress(hinstLib, "engEvalString");
		pengOutputBuffer	= (hengOutputBuffer)	GetProcAddress(hinstLib, "engOutputBuffer");
	}
}

CMatlabEng::~CMatlabEng()
{
	if (pEng!=NULL)
	{
		Close();
	}
	if(hinstLib != NULL)
	{
		FreeLibrary(hinstLib);
		hinstLib = NULL;	
	}
}

int CMatlabEng::Open(const char *StartCmd)
{
	pEng=pengOpen(StartCmd);
	if (pEng==NULL)
		return -1;
	else
		return 0;
}

int CMatlabEng::Close()
{
	int Result=pengClose(pEng);
	if (Result==0)	//Success
		pEng=NULL;

	return Result;
}

int CMatlabEng::EvalString(const char *string)
{
	return (pengEvalString(pEng, string));
}

mxArray* CMatlabEng::GetVariable(const char *name)
{
	return (pengGetVariable(pEng, name));
}


int CMatlabEng::GetVisible(bool* value)
{
	return (pengGetVisible(pEng, value));
}

void CMatlabEng::OpenSingleUse(const char *startcmd, void *dcom, int *retstatus)
{
	pEng=pengOpenSingleUse(startcmd, dcom, retstatus);
}

int CMatlabEng::OutputBuffer(char *p, int n)
{
	return (pengOutputBuffer(pEng, p, n));
}

int CMatlabEng::PutVariable(const char *name, const mxArray *mp)
{
	return (pengPutVariable(pEng, name, mp));
}

int CMatlabEng::SetVisible(bool value)
{
	return (pengSetVisible(pEng, value));
}
